package com.example.starapp.starfrag;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.starapp.R;
import com.example.starapp.custom.DLRoundImageView;
import com.example.starapp.utils.AssetsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StarAnalysisActivity extends AppCompatActivity implements View.OnClickListener {
    StarInfo.StarinfoBean starinfoBean;
    TextView titleTv,nameTv,dateTv,footerTv;
    ImageView backIv;
    ListView starLv;
    DLRoundImageView logoIv;
    List<AnalysisBean> mDatas;
    private AnalysisBaseAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_star_analysis);
//        获取上一个界面传递过来的数据
        Intent intent = getIntent();
        starinfoBean = (StarInfo.StarinfoBean) intent.getSerializableExtra("starinfo");
//        初始化控件
        initView();
        mDatas = new ArrayList<>();
        adapter = new AnalysisBaseAdapter(this, mDatas);
        starLv.setAdapter(adapter);
        addDataToList(starinfoBean);
        showData(starinfoBean);
    }
    /* 在控件上显示数据的函数*/
    private void showData(StarInfo.StarinfoBean bean) {
        titleTv.setText("星座详情");
        nameTv.setText(bean.getName());
        dateTv.setText(bean.getDate());
        Map<String, Bitmap> logoMap = AssetsUtils.getContentLogoImgMap();
        Bitmap bitmap = logoMap.get(bean.getLogoname());
        logoIv.setImageBitmap(bitmap);
        footerTv.setText(bean.getInfo());
    }

    /* 添加数据到集合当中*/
    private void addDataToList(StarInfo.StarinfoBean bean) {
        AnalysisBean ab1 = new AnalysisBean("性格特点 :", bean.getTd(), R.color.lightblue);
        AnalysisBean ab2 = new AnalysisBean("掌管宫位 :", bean.getGw(), R.color.orange);
        AnalysisBean ab3 = new AnalysisBean("显阴阳性 :", bean.getYy(), R.color.purple);
        AnalysisBean ab4 = new AnalysisBean("最大特征 :", bean.getTz(), R.color.lightgreen);
        AnalysisBean ab5 = new AnalysisBean("主管星球 :", bean.getZg(), R.color.darkblue);
        AnalysisBean ab6 = new AnalysisBean("幸运颜色 :", bean.getYs(), R.color.lightred);
        AnalysisBean ab7 = new AnalysisBean("搭配珠宝 :", bean.getZb(), R.color.darkgreen);
        AnalysisBean ab8 = new AnalysisBean("幸运号码 :", bean.getHm(), R.color.colorPrimary);
        AnalysisBean ab9 = new AnalysisBean("相配金属 :", bean.getJs(), R.color.pink);
        mDatas.add(ab1);
        mDatas.add(ab2);
        mDatas.add(ab3);
        mDatas.add(ab4);
        mDatas.add(ab5);
        mDatas.add(ab6);
        mDatas.add(ab7);
        mDatas.add(ab8);
        mDatas.add(ab9);
        adapter.notifyDataSetChanged();  //数据源发生变化，提示适配器重新更新数据
    }

    /* 初始化控件的函数 */
    private void initView() {
        titleTv = findViewById(R.id.title_tv);
        backIv = findViewById(R.id.title_iv_back);
        backIv.setOnClickListener(this);
        starLv = findViewById(R.id.staranalysis_lv);
        nameTv = findViewById(R.id.staranalysis_tv_name);
        dateTv = findViewById(R.id.staranalysis_tv_date);
        logoIv = findViewById(R.id.staranalysis_iv);
//        在ListView的底部添加底部布局
        View footerView = LayoutInflater.from(this).inflate(R.layout.footer_star_analysis, null);
        starLv.addFooterView(footerView);
        footerTv = footerView.findViewById(R.id.footerstar_tv_info);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
        }
    }
}